import {toHome} from '@/utils/pageRedirect'
import {observable, action} from 'mobx'
import PageQuery from './extends/pageQuery'
import Pagination from './extends/pagination'

class ChildrenPageUtils implements PageQuery, Pagination {
  @observable canUpDate: boolean = true

  @observable pagination = {
    page: 1,
    size: 20,
    total: 0,
    totalPage: 0
  }

  @action.bound
  updatePagination({
    page,
    size = 20,
    total,
    totalPage
  }: {
    page: number
    size?: number
    total: number
    totalPage: number
  }) {
    this.pagination.page = page
    this.pagination.size = size
    this.pagination.total = total
    this.pagination.totalPage = totalPage || Math.ceil(total / size)
  }

  @action.bound
  addPage(fn: (page: number, size: number) => void) {
    const page = this.pagination.page
    const totalPage = this.pagination.totalPage

    if (page >= totalPage) {
      this.closeUpdate()
      this.pagination.page = totalPage
    } else {
      this.pagination.page = page + 1
      this.openUpdate()
      fn && fn(this.pagination.page, this.pagination.size)
      console.log('%c历史记录继续加载', 'color:green')
      console.log('当前页码 和 总页码', this.pagination.page, totalPage)
    }
  }

  @action.bound
  delPage() {
    const page = this.pagination.page
    if (page <= 0) {
      this.closeUpdate()
      this.pagination.page = 1
    } else {
      this.openUpdate()
      this.pagination.page = page - 1
    }
  }

  @action.bound
  resetPagination() {
    this.openUpdate()
    this.pagination.page = 1
    this.pagination.size = 20
    this.pagination.totalPage = 0
  }

  @action.bound
  closeUpdate() {
    console.log('%c关闭分页加载', 'color:red')
    this.canUpDate = false
  }
  @action.bound
  openUpdate() {
    console.log('%c开启分页加载', 'color:green')
    this.canUpDate = true
  }

  @observable pageQuery: Partial<Record<string, string>> = {}

  /**
   * 用于跳转页面后校验是否携带设备Id
   * @param params 携带的路由参数对象
   * @param update 是否持续更新pageQuery。若为undefined或者false，只有当页面携带的设备Id与之前不同时才进行更新pageQuery；若为true，不管页面携带的设备Id是否与之前一样，都会更新pageQuery
   * @returns
   */
  @action.bound
  updatePageQuery(params: Partial<Record<string, string>>, update?: boolean) {
    if (!params.devId) {
      toHome()
    } else {
      if (!update && params.devId === this.pageQuery.devId) {
        console.log('%c updatePageQuery same', 'color:yellow')
        return 'same'
      } else {
        console.log('%c updatePageQuery udpate', 'color:green')
        console.log('当前页面路由携带参数', params)
        this.pageQuery = params
        return 'diff'
      }
    }
  }

  /**用于其他页面获取路由参数 */
  @action.bound
  getPageQuery<T = Object>(params: T, paramNameList: string[]): T | null {
    if (params && params instanceof Object && paramNameList) {
      const paramsNameList = Object.keys(params)
      for (let i = 0; i < paramNameList.length; i++) {
        const paramName = paramNameList[i]
        if (!paramsNameList.includes(paramName)) return null
      }
      this.pageQuery = params

      return params
    } else {
      return null
    }
  }

  @action.bound
  clearPageQuery() {
    // 清空页面路由参数缓存
    this.pageQuery = {}
  }
}

export default ChildrenPageUtils
